<?php
include('main.php');
include(INC_DIR.'header.php'); 
?>
<?php
	try
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{	
			if ($_SESSION["searchresults"] != '')
			{
				$to = getEmail();
				//echo $to;
				$subject = "Service request";
				$body = "Service request from:\nName:" . $_POST["nameField"] . "\nPhone Number:" . $_POST["phoneNumberField"] 
					. "\nEmail:" . $_POST["emailField"] . "\nComments:" . $_POST["commentsField"] . "\n" . $_SESSION["searchresults"] ;
				$body = $body . "\nAirports Selected:";
				if(is_array($_SESSION['userAirportSelections'] ))
				{
					foreach ($_SESSION['userAirportSelections'] as $key=>$value)
					{
						$body = $body . "\n$value";
					}
				}
			 if (mail($to, $subject, $body)) 
				$_SESSION["emailResult"] = "<p>Message successfully sent!</p>";
			 else 
				$_SESSION["emailResult"] = "<p>Message delivery failed...</p>";	
			}
		}
		else
			$_SESSION['provider'] = $_GET['provider'];
		
	}
	catch (Exception $e) 
	{
		echo 'Caught exception: ',  var_dump($e->getMessage()), "\n";
	}		
	function getEmail()
	{
		include('dbcon.php');
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db(MYSQLDB, $con);
		$qry = 'SELECT email '.
			'from companies where id = ' . $_SESSION['provider'];
		$result = mysql_query($qry) or die ('query error, contact:getemail() - ERROR: ' . mysql_error() . ' query:');
		while($row = mysql_fetch_array($result))
		{
			$email = $row["email"];
		}	

		mysql_close($con);	
		return $email;
	}
	
?>
<?php
include(INC_DIR.'footer.php'); 
//include('teststuff.php'); 
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?mode=1" method="post">
<div id="inputFields" class="centerDiv">
Name: <br><input type="text" id="fullName" class="textInput" name="nameField" value=""> <br>
Phone Number:<br> <input type="text" id="phoneNumber" class="textInput" name="phoneNumberField" value=""> <br>
Email Address: <br><input type="text" id="email" class="textInput" name="emailField" value=""> <br>
Comments: <br><input type="text" id="comments" class="textInput" name="commentsField" value="">
<br>
<br>
<?php
echo $_SESSION["emailResult"];
?>
<input type="submit" value="Submit Request">
</div>
</form>
</body>
</html>

</html>